package com.bw.sofa_module.contentsofa;

import com.bw.common.entity.LiveDataEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.network_sdk.entity.BaseEntity;
import com.bw.network_sdk.videoentity.VideoEntity;
import com.bw.sofa_module.entity.diss.DissEntity;
import com.bw.sofa_module.entity.likecount.LikeCountEntity;
import com.bw.sofa_module.entity.likestate.LikeStateEntity;
import com.bw.sofa_module.entity.likestate.LikesStateEntity;
import com.bw.sofa_module.entity.likestate.LikesStateValueEntity;
import com.bw.sofa_module.sofa.SofaModel;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ContentViewModel extends BaseViewModel<SofaModel> {
    private long itemId;
    private int userId = 10;
    public int count = 0;
    public void onLike(int itemId, int userId) {
        this.itemId = itemId;
        mModel.requestLikeState(itemId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void onDiss(long itemId) {
        mModel.requestDiss(itemId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void initData() {
        Map<String,Object> map = new HashMap<>();
        map.put("feedId",0);
        map.put("feedType","text");
        map.put("pageCount",10);
        map.put("userId",10);
        mModel.request(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void onRestle(BaseEntity entity) {
        if (entity instanceof VideoEntity){
            VideoEntity videoEntity = (VideoEntity) entity;
            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.START_VIEW_MODEL;
            Map<LiveDataEntity.LiveDataType,Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY,videoEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }else if (entity instanceof LikeStateEntity) {
            LikeStateEntity likeEntity = (LikeStateEntity) entity;
            if (likeEntity.data.data.hasLiked) {
                mModel.requestLikeCount(itemId)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this);
            } else {
                LikeStateEntity likeStateEntity = new LikeStateEntity();
                LikesStateEntity likesStateEntity = new LikesStateEntity();
                LikesStateValueEntity likesStateValueEntity = new LikesStateValueEntity();
                likesStateValueEntity.hasLiked = false;
                likesStateEntity.data = likesStateValueEntity;
                likeStateEntity.data = likesStateEntity;

                LiveDataEntity liveDataEntity = new LiveDataEntity();
                liveDataEntity.action = LiveDataEntity.LiveDataType.LIKES;
                Map<LiveDataEntity.LiveDataType, Object> map = new HashMap<>();
                map.put(LiveDataEntity.LiveDataType.ENTITY, likeStateEntity);
                liveDataEntity.data = map;
                liveDataViewModel.post(liveDataEntity);
            }
        } else if (entity instanceof LikeCountEntity) {
            LikeCountEntity likeCountEntity = (LikeCountEntity) entity;
            count = likeCountEntity.data.data.count;
            LikeStateEntity likeStateEntity = new LikeStateEntity();
            LikesStateEntity likesStateEntity = new LikesStateEntity();
            LikesStateValueEntity likesStateValueEntity = new LikesStateValueEntity();
            likesStateValueEntity.hasLiked = true;
            likesStateEntity.data = likesStateValueEntity;
            likeStateEntity.data = likesStateEntity;

            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.LIKES;
            Map<LiveDataEntity.LiveDataType, Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY, likeStateEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        } else if (entity instanceof DissEntity) {
            DissEntity dissEntity = (DissEntity) entity;
            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.DISS;
            Map<LiveDataEntity.LiveDataType, Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY, dissEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }
    }
}
